package cn.jiedh.app.meiweidk.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jiedh.app.meiweidk.MainActivity;
import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.tool.ActivityUtil;
import cn.jiedh.app.meiweidk.Utils.tool.SPUtils;
import cn.jiedh.app.meiweidk.Utils.tool.StringUtil;
import cn.jiedh.app.meiweidk.Utils.tool.T;
import cn.jiedh.app.meiweidk.Utils.tool.UrlUtils;
import cn.jiedh.app.meiweidk.base.BaseActivity;
import cn.jiedh.app.meiweidk.login.LoginActivity;

/**
 * @name：修改登录密码
 * @author：司马林
 * @phone：18423134135
 * @createTime：2017/10/20
 */

public class ChangePswActivity extends BaseActivity {
    @BindView(R.id.bar_tv_title)
    TextView title;
    @BindView(R.id.old_psw_edt)
    EditText oldPswEdt;
    @BindView(R.id.old_show_psw)
    CheckBox oldShowPswCheckBox;
    @BindView(R.id.new_psw_edt)
    EditText newPswEdt;
    @BindView(R.id.new_show_psw)
    CheckBox newShowPswCheckBox;
    @BindView(R.id.coordinator)
    CoordinatorLayout coordinatorLayout;
    @BindView(R.id.config)
    Button configBtn;


    @Override
    protected int getLayoutView() {
        return R.layout.activity_change_psw;
    }

    @Override
    protected void initView(View view) {
        title.setText("修改登录密码");
        newPswEdt.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                config(oldPswEdt.getText().toString(), newPswEdt.getText().toString());
            }
            return false;
        });

        oldShowPswCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                oldPswEdt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                oldPswEdt.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        });
        newShowPswCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                newPswEdt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                newPswEdt.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.config)
    public void onClick() {
        MobclickAgent.onEvent(activity,"my_setting_changepwd_confirm_click");
        InputMethodManager imm = (InputMethodManager) configBtn.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(configBtn.getWindowToken(), 0);
        config(oldPswEdt.getText().toString(), newPswEdt.getText().toString());
    }

    private void config(String oldPsw, String newPsw) {
        if (StringUtil.isPassword(oldPsw) && StringUtil.isPassword(newPsw)) {
            Map<String, Object> map = new HashMap<>();
            map.put("oldPassword", oldPsw);
            map.put("newPassword", newPsw);
            request(this, UrlUtils.UPDATAPASSWORD, map, true, response -> {
                T.showShort(response.getMessage());
                if (0 == response.getCode()) {
                    SPUtils.clearUser(this);
                    Intent intent = new Intent(this, LoginActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("isGoPswLogin", true);
                    bundle.putBoolean("isGoHome", true);
                    bundle.putInt("update2login",3);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, 1);
                    MobclickAgent.onEvent(activity,"my_setting_changepwd_confirm_click");
                    SPUtils.saveBuried("click","0","设置-密码修改-确定修改按钮点击","",
                            "我的-设置-密码修改","","","");
                }
            });
        } else {
            T.showShortCenterToast("请输入6-18位数字和字母组合的密码");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == LoginActivity.RESULT_CODE) {
            ActivityUtil.next(ChangePswActivity.this, MainActivity.class);
        }
    }
}
